<div class="widget">
	<div class="widget-header bg-blue">
		<i class="widget-icon icon-notebook"></i> 
		<span class="widget-caption"> 数据字典 </span>
		<div class="widget-buttons">
			<a href="javascript:void(0);" onclick="handlerCancel(this);" title="关闭"
				 data-toggle="dispose" data-refresh-table="#table-codec"> <i class="fa fa-times"></i> </a> 
		</div>
	</div>
	<div class="widget-body">
		<!-- TODO 基本信息 -->
		<div class="widget radius-bordered">
			<div class="widget-header">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> 基本信息 </span>
			</div>
			<div class="widget-body editor-body-sm editor-body-codec">
				
			</div>
		</div>
		
		<!-- TODO 校区信息 -->
		<div class="widget radius-bordered" id="editor-panel-codesub">
			<div class="widget-header">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> 字典列表 </span>
				<div class="widget-buttons buttons-bordered">
					<button class="btn btn-default btn-sm" title="添加" data-toggle="tooltip" data-placement="top" onclick="handlerAddRow('#table-codesub')">
						<i class="fa fa-plus"></i>
					</button>
				</div>
			</div>
			<div class="widget-body editor-body-sm">
				<form class="table-editor" id="tables-form-codesub">
					<table data-ajax="handlerSearch" 
								 data-method="post" 
								 data-id-field="id" 
								 data-unique-id="id" 
								 data-pagination="true"  
								 data-check-on-init="true" 
								 data-click-to-select="true" 
								 data-side-pagination="server" 
								 data-mobile-responsive="true" 
								 data-form-source="#tables-form-codesub" 
								 data-form-target="#editor-form-codesub" 
								 data-form-search="#search-form-codesub" 
								 id="table-codesub">
						<thead>
							<tr>
								<th data-radio="true"></th>
								<th data-field="id" data-visible="false"></th>
								<th data-field="name" 		data-align="center">名称</th>
								<th data-field="code" 		data-align="center">编码</th>
								<th data-field="sort" 		data-align="center">排序</th>
								<th data-field="operator" data-align="center" data-width="65" data-formatter="renderOperator" data-table-target="codesub">操作</th>
							</tr>
						</thead>
					</table>
				</form>
			</div>
		</div>
	</div>
</div>

<!-- 提交表单 ========================================================= -->
<form id="editor-form-codec" action="/miwawa/default/common/update" method="post" 
			name="com.app.miwawa.api.system.entity.config.Codec">
			<input type="hidden" name="bean['text']" id="text">
			<input type="hidden" name="bean['status']" value="0">
			<input type="hidden" name="bean['invalid']" value="F">
</form>
<form id="editor-form-codesub" action="/miwawa/default/common/update" method="post" 
			name="com.app.miwawa.api.system.entity.config.Codec">
			<input type="hidden" name="bean['pid']" 		id="pid">
			<input type="hidden" name="bean['pcode']" 	id="pcode">
			<input type="hidden" name="bean['text']" 		id="text">
			<input type="hidden" name="bean['status']" 	value="0">
			<input type="hidden" name="bean['invalid']" value="F">
</form>
<!-- 查询表单 ========================================================= -->
<form id="search-form-codesub" action="/miwawa/default/common/search.json" method="post" 
			name="com.app.miwawa.api.system.entity.config.Codec">
			<input type="hidden" name="pid" value="0" data-search-group="and" data-search-operation="EQ">
			<input type="hidden" name="status" value="0" data-search-group="and" data-search-operation="EQ">
			<input type="hidden" name="invalid" value="F" data-search-group="and" data-search-operation="EQ">
</form>
    
<script type="text/javascript">
<!--
	require(['domReady'],function(domReady){
		domReady(function () {
			loadData();
    });
	});
	
	function loadData(){
		var codec = jQuery("#table-codec").data("selectedRow");
		jQuery(":hidden[name='pid'], #pid").val(codec.id);
		
		jQuery(".editor-body-codec").loadform("load", {
			templateId: "#view-codec-template",
			templatePath: "/miwawa/content/system/codec/template.html",
			templateData: codec,
			onLoadSuccess: function(){
				require(['jquery.plugin.form'],function(form){
					form.tooltip("[data-toggle='tooltip']");
				});
			}
		});
		
		jQuery("#table-codesub").bootstrapTable({
			onLoadSuccess:function(){
				jQuery("[data-toggle='tooltip']").tooltip({html:true, container:"html", trigger:'hover'});
			}
		});
	}
	
	function setFormData(dest, orig){
		jQuery("#pcode",orig).val(jQuery("#code",".form-editor-codec").val());
		jQuery("#text",orig).val(jQuery("#name",dest).val());
		return true;
	}
	
	function handlerAddRow(target){
		var $table = jQuery(target);
		
		if(!hasEditorRow()){
			var rowdata = {
				id:   'new',
        name: createInput('name', 'text', 'ext-text'),
        code: createInput('code', 'text', 'ext-text'),
        sort: createInput('sort',	'text', 'ext-text', '0'),
        operator: createButton(target)
			};
			
			$table.bootstrapTable('insertRow', { index: 0, row: rowdata });
		}
		
		// 判断是否有编辑行
		function hasEditorRow(){
			return $table.bootstrapTable('getRowByUniqueId', 'new') ? true : false;
		}
	}
	
	function handlerEditRow(trigger, index){
		var _opts = jQuery(trigger).data();
		var $table = jQuery(_opts.tableSource);
		
		$table.bootstrapTable("updateRow",{
			index: index,
			row: createEditRow(_opts.rowdata, _opts.tableSource)
		});
		
		function createEditRow(row, target){
			jQuery(target).append("<input type='hidden' name='id' value='"+row.id+"'>");
			
			return {
				id:   "edit",
	      name: createInput('name', 'text', 'ext-text', row.name),
	      code: createInput('code', 'text', 'ext-text', row.code),
	      sort: createInput('sort',	'text', 'ext-text', row.sort),
	      operator: createButton(target)
			};
		}
		
		jQuery("[data-toggle='tooltip']").tooltip({html:true, container:"html", trigger:'hover'});
	}
	
	// 创建输入框
	function createInput(name, type, ext, value){
		switch(type){
			case 'text': 
				var input = "";
				if(value != undefined){
					input = '<input name="'+name+'" id="'+name+'" type="'+type+'" class="form-control '+ext+'" value="'+value+'">';
				}else{
					input = '<input name="'+name+'" id="'+name+'" type="'+type+'" class="form-control '+ext+'">';
				}
			return input;
		}
	}
	
	// 创建按钮
	function createButton(target){
		return ""
					+"<span class='table-editor-tools'>"
					+"	<button type='button' class='btn btn-danger  btn-xs' onclick='handlerCancelRow(\""+target+"\", true);' data-toggle='tooltip' data-placement='top' title='取消'><i class='icon ion-close-circled'></i></button>"
					+"	<button type='button' class='btn btn-success btn-xs' onclick='handlerSubmitRow(\""+target+"\",\"setFormData\");' data-toggle='tooltip' data-placement='top' title='确认'><i class='icon ion-checkmark-circled'></i></button>"
					+"</span>";
	}
//-->
</script>